<?php
add_action('widgets_init', 'Reedwan_Magazine2_load_widgets');

function Reedwan_Magazine2_load_widgets()
{
	register_widget('Reedwan_Magazine2_Widget');
}

class Reedwan_Magazine2_Widget extends WP_Widget {
	
	function Reedwan_Magazine2_Widget()
	{
		$widget_ops = array('classname' => 'reedwan_magazine2', 'description' => 'Column magazine recent posts widget for magazine widget.');

		$control_ops = array('id_base' => 'reedwan_magazine2-widget');

		$this->WP_Widget('reedwan_magazine2-widget', 'Column Magazine 2', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		extract($args);
		
		$title = $instance['title'];
		$post_type = 'all';
		$categories = $instance['categories'];
		$posts = $instance['posts'];
		$show_image = isset($instance['show_image']) ? 'true' : 'false';
		echo $before_widget;
		?>
		
		<?php
		$post_types = get_post_types();
		unset($post_types['page'], $post_types['portfolio'], $post_types['gallery'], $post_types['reviews'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
		
		if($post_type == 'all') {
			$post_type_array = $post_types;
		} else {
			$post_type_array = $post_type;
		}
		?>
			<?php
			if($title) {
				echo $before_title;
				echo '<a href="';
				echo get_category_link( $categories );
				echo '">';
				echo $title;
				echo '</a>';
				echo $after_title;
			}
			?>
			<?php
			$recent_posts = new WP_Query(array(
				'showposts' => $posts,
				'post_type' => $post_type_array,
				'cat' => $categories,
			));
			?>
			<?php
			$counter = 1; 
			while($recent_posts->have_posts()): $recent_posts->the_post(); 
			if(has_post_format('video')) 
			{
				$format_icon = 'class="video-format-icon"';
				$format_icon_small = ' (Video)';
			}
			else if (has_post_format('audio'))
			{
				$format_icon = 'class="audio-format-icon"';
				$format_icon_small = ' (Audio)';
			}
			else if (has_post_format('gallery'))
			{
				$format_icon = 'class="gallery-format-icon"';
				$format_icon_small = ' (Gallery)';
			}
			else {
				$format_icon = 'class="standard-format-icon"';
				$format_icon_small = '';
			}
			
			$review_show =  get_post_meta(get_the_ID(), 'reedwan_show_review', true);
			$overall_percentage =  get_post_meta(get_the_ID(), 'reedwan_overall_score', true);
				
			?>
			<?php if($counter == 1): ?>
			
			
				<?php if(has_post_thumbnail()): ?>
				<div class="content-big-image">
					<a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_post_thumbnail('content-magz', array('class' => 'overlay'));?></a>	
					<?php if ($review_show == 'Yes' && $overall_percentage!=0) { ?>
						<span class="content-score"><?php echo $overall_percentage; ?>%</span>
					<?php } ?>
					
					<div class="content-big-desc">
						<h3><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h3>
						<div class="clear"></div>
						<span class="magz-meta"><?php the_time('F j, Y'); ?>  |  <?php comments_number(__( '0 Comments', 'unicorn'), __('1 Comments', 'unicorn'), __('% Comments', 'unicorn'));?></span>
						<p><?php echo string_limit_words(get_the_excerpt(), 30); ?></p>
						<a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>' class="readmore"><?php _e('Read More ', 'unicorn'); ?>&rarr;</a>
					</div>
					
					<div class="big-title">
						<h3><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h3>
						<div class="clear"></div>
						<span class="magz-meta"><?php the_time('F j, Y'); ?>  |  <?php comments_number(__( '0 Comments', 'unicorn'), __('1 Comments', 'unicorn'), __('% Comments', 'unicorn'));?></span>
					</div>
				</div>
				<?php else: ?>
				<div class="content-big-image">
					<a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo get_template_directory_uri(); ?>/images/thumbnails/no-image-content-magz.png" alt="no image" class="overlay"/></a>
					<?php if ($review_show == 'Yes' && $overall_percentage!=0) { ?>
						<span class="content-score"><?php echo $overall_percentage; ?>%</span>
					<?php } ?>
					<div class="content-big-desc">
						<h3><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h3>
						<div class="clear"></div>
						<span class="magz-meta"><?php the_time('F j, Y'); ?>  |  <?php comments_number(__( '0 Comments', 'unicorn'), __('1 Comments', 'unicorn'), __('% Comments', 'unicorn'));?></span>
						<p><?php echo string_limit_words(get_the_excerpt(), 30); ?></p>
						<a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>' class="readmore"><?php _e('Read More ', 'unicorn'); ?>&rarr;</a>
					</div>
				</div>
				<?php endif; ?>
				<div class="clear"></div>
			<?php else: ?>
			<?php if($show_image == 'true'): ?>
			<div class="block-small<?php if($counter%2 != 0) { echo " half-right"; } else { echo " half-left"; }?>">
				<?php if(has_post_thumbnail()): ?>
				<div class="magz-image small">
					<a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_post_thumbnail('small-magz', array('class' => 'overlay'));?></a>
					<?php if ($review_show == 'Yes' && $overall_percentage!=0) { ?>
						<span class="content-score"><?php echo $overall_percentage; ?>%</span>
					<?php } ?>
					
				</div>
				<?php else: ?>
				<div class="magz-image small">
					<a <?php echo $format_icon; ?> href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo get_template_directory_uri(); ?>/images/thumbnails/no-image-small-magz.png" alt="no image" class="overlay"/></a>
					<?php if ($review_show == 'Yes' && $overall_percentage!=0) { ?>
						<span class="content-score"><?php echo $overall_percentage; ?>%</span>
					<?php } ?>
				</div>
				<?php endif; ?>
				<div class="small-desc">
					<h3 class="desc-title"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h3>
					<div class="magz-meta"><?php the_time('F j, Y'); ?>  |  <?php comments_number(__( '0 Comments', 'unicorn'), __('1 Comments', 'unicorn'), __('% Comments', 'unicorn'));?></div>
				</div>
			</div>
			<?php if($counter%2 != 0) {echo'<div class="clear"></div>';}?>
			<?php endif; ?>
			<?php if($show_image == 'false'): ?>
			<div class="block-small-noimage<?php if($counter%2 != 0) { echo " half-right"; } else { echo " half-left"; }?>">
				<h3 class="desc-title"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?><?php echo $format_icon_small; ?></a>
				<?php if ($review_show == 'Yes' && $overall_percentage!=0) { ?>
						<span class="review-text"> review</span>
					<?php } ?>
				</h3>
			</div>
			<?php if($counter%2 != 0) {echo'<div class="clear"></div>';}?>
			<?php endif; ?>
			<?php endif; ?>
			<?php $counter++; endwhile; ?>
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		
		$instance['title'] = $new_instance['title'];
		$instance['post_type'] = 'all';
		$instance['categories'] = $new_instance['categories'];
		$instance['posts'] = $new_instance['posts'];
		$instance['show_image'] = $new_instance['show_image'];
		return $instance;
	}

	function form($instance)
	{
		$defaults = array('title' => 'Recent Posts', 'post_type' => 'all', 'categories' => 'all', 'posts' => 5, 'show_image'=>null );
		$instance = wp_parse_args((array) $instance, $defaults); ?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">Title</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('categories'); ?>">Filter by Category</label> 
			<select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" class="widefat categories" style="width:100%;">
				<option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>>all categories</option>
				<?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
				<?php foreach($categories as $category) { ?>
				<option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('posts'); ?>">Number of posts</label>
			<input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
			
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked($instance['show_image'], 'on'); ?> id="<?php echo $this->get_field_id('show_image'); ?>" name="<?php echo $this->get_field_name('show_image'); ?>" /> 
			<label for="<?php echo $this->get_field_id('show_image'); ?>">Show thumbnail image</label>
		</p>
		
	<?php }
}
?>